package _5thread;

import _0util.HttpUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.*;

/**
 * @Description:
 * @Author wangzhen
 * @Date 2022-06-10 22:49
 **/
public class CompletableFutureTest2 {

    public static void main(String[] args) throws ExecutionException, InterruptedException {

        test1();

    }

    public static void test1(){
        ExecutorService threadPool = Executors.newFixedThreadPool(5);

        CompletableFuture.supplyAsync(() ->{
            System.out.println(Thread.currentThread().getName()+"线程1执行，返回结果:"+2);
            return 2;

        },threadPool).thenApply((v) ->{

            try {
                Thread.sleep(1000);
            }catch (Exception e){
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName()+"线程2执行，拿到参数:"+v);
            return v+2;

        }).whenComplete((v,e)->{
            System.out.println(Thread.currentThread().getName()+"最终处理，结果:"+v);
            System.out.println("最终处理，异常:"+e);
        });

        threadPool.shutdown();
    }


    public static void test2() throws ExecutionException, InterruptedException {
        ExecutorService threadPool = Executors.newFixedThreadPool(5);

        try {

            CompletableFuture<Integer> thread1 = CompletableFuture.supplyAsync(() -> {

                int value=(int)(Math.random()*10);
                System.out.println(Thread.currentThread().getName() + "执行减库存，返回结果:" + value);
                return value;

            }, threadPool);

            CompletableFuture<Integer> thread2 = CompletableFuture.supplyAsync(() -> {

                int i=10/0;
                int value=(int)(Math.random()*10);
                System.out.println(Thread.currentThread().getName() + "执行扣款，返回结果:" + value);
                return value;

            }, threadPool);


            CompletableFuture<Boolean> result = thread1.thenCombineAsync(thread2, (v1, v2) -> {

                System.out.println(Thread.currentThread().getName() + "执行,v1参数为:" + v1 + ",v2参数为:" + v2);
                if (v1 + v2 > 5) {
                    System.out.println(Thread.currentThread().getName() + "下单成功");
                    return true;

                } else {
                    System.out.println(Thread.currentThread().getName() + "下单成功");
                    return false;
                }

            }, threadPool);



        }catch (Exception e){
            e.printStackTrace();
            System.out.println(Thread.currentThread().getName() +",发生错误:"+e.getMessage());//main,发生错误:java.lang.ArithmeticException: / by zero

        }finally {
            threadPool.shutdown();
        }

    }



    public static void test3(){
        ExecutorService threadPool = Executors.newFixedThreadPool(5);


        try {

            CompletableFuture<String> thread1 = CompletableFuture.supplyAsync(() -> {

                String json = HttpUtil.get("http://pv.sohu.com/cityjson", null, null);
                System.out.println(Thread.currentThread().getName() + "查询当前IP，返回结果:" + json);
                return json;

            }, threadPool);

            CompletableFuture<String> thread2 = CompletableFuture.supplyAsync(() -> {

                String json = HttpUtil.get("http://pv.sohu.com/cityjson", null, null);
                System.out.println(Thread.currentThread().getName() + "查询当前IP，返回结果:" + json);
                return json;

            }, threadPool);

            CompletableFuture<String> thread3 = CompletableFuture.supplyAsync(() -> {

                String json = HttpUtil.get("https://api.ip.sb/ip", null, null);
                System.out.println(Thread.currentThread().getName() + "查询当前IP，返回结果:" + json);
                return json;

            }, threadPool);


            Object result = CompletableFuture.anyOf(thread1, thread2, thread3).get();
            System.out.println(Thread.currentThread().getName()+"最终执行结果:"+result.toString());


        }catch (Exception e){
            e.printStackTrace();
            System.out.println(Thread.currentThread().getName() +",发生错误:"+e.getMessage());

        }finally {
            threadPool.shutdown();
        }

    }
}
